package logic

import (
	"context"
	"fmt"
	"wechat/models/mysql"

	"wechat/userrpc/internal/svc"
	"wechat/userrpc/userrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type GroupListLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewGroupListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GroupListLogic {
	return &GroupListLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 分组列表
func (l *GroupListLogic) GroupList(in *userrpc.GroupListRequest) (*userrpc.GroupListResponse, error) {
	// todo: add your logic here and delete this line
	var user mysql.User

	mysql.DB.Where("id = ?", in.UserId).Find(&user)

	if user.ID == 0 {
		return nil, fmt.Errorf("当前用户不存在")
	}

	var groups []mysql.Group

	mysql.DB.Debug().Where("user_id = ?", in.UserId).Find(&groups)

	var groupLists []*userrpc.GroupList

	for _, group := range groups {
		list := userrpc.GroupList{
			UserId: group.UserId,
			Id:     int64(group.ID),
			Name:   group.Name,
		}

		groupLists = append(groupLists, &list)
	}

	return &userrpc.GroupListResponse{List: groupLists}, nil
}
